package com.example.a33mvvm;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;


import android.content.Intent;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.a33mvvm.adapter.ViewPager2Adapter;
import com.example.a33mvvm.databinding.ActivityMainBinding;
import com.example.a33mvvm.fragment_viewpage.HomeFragment;
import com.example.a33mvvm.fragment_viewpage.MessageFragment;
import com.example.a33mvvm.fragment_viewpage.MineFragment;
import com.example.a33mvvm.fragment_viewpage.VideoFragment;
import com.example.a33mvvm.liu.LiuActivity;
import com.example.base_mvvm.base.view.BaseActivity;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding>{
    private List<Fragment> fragments = new ArrayList<>();
    private ViewPager2Adapter viewPager2Adapter;
    @Override
    protected void initView() {
        requestPermissions(new String[]{RECORD_AUDIO,CAMERA,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE},101);
        dataBinding.vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        dataBinding.bottomNav.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        dataBinding.bottomNav.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        dataBinding.bottomNav.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        dataBinding.bottomNav.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });
        dataBinding.bottomNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        dataBinding.vp2.setCurrentItem(0);
                        break;
                    case R.id.video:
                        dataBinding.vp2.setCurrentItem(1);
                        break;
                    case R.id.fa_bu:
                        Intent intent = new Intent(MainActivity.this, LiuActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.message:
                        dataBinding.vp2.setCurrentItem(2);
                        break;
                    case R.id.mine:
                        dataBinding.vp2.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
    }
    @Override
    protected void initData() {
        fragments.clear();
        fragments.add(new HomeFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MineFragment());
        Log.d("sss", "initData: "+fragments.size());
        viewPager2Adapter = new ViewPager2Adapter(this,fragments);
        dataBinding.vp2.setAdapter(viewPager2Adapter);
    }



    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}